<template>
	<view class="box">
		<view class="header">
			<u-navbar :autoBack="true" bgColor="transparent" :fixed="false">
				<template v-slot:center>
					<view class="search">
						<view class="icon_left">
							<u-icon name="search" color="#B1B2B2" size="28"></u-icon>
						</view>
						<view class="sea">
							<u--input placeholder="搜索设备" border="surround" v-model="listData.querySearchWords"
								@confirm="confirm" @blur="confirm"></u--input>
						</view>
						<view class="icon_right" @click="clear">
							<u-icon name="close-circle-fill" color="#B1B2B2" size="20"
								v-if="listData.querySearchWords.length > 0"></u-icon>
						</view>
					</view>
				</template>
			</u-navbar>
		</view>
		<view class="box_content">
			<view class="whole">
				<uni-segmented-control :current="Number(current)" :values="items" @clickItem="onClickItem"
					styleType="text" activeColor="#2590FA"></uni-segmented-control>
			</view>
			<view class="device">
				<scroll-view :scroll-y="true" :refresher-enabled="true" :refresher-threshold="0"
					@refresherrefresh="refresherrefresh" style="height: 100%;overflow: auto; "
					:refresher-triggered="refresherTriggered" @scrolltolower="scrolltolower" :lower-threshold="20">
					<view class='content'>
						<u-transition :show="true" v-for="item in list" :key="item.id" mode="slide-left">
							<DeviceList :listData="item" @getBlur="getBlur" @getuse="getuse" @getRecharge="getRecharge"></DeviceList>
						</u-transition>
					</view>
					<u-loadmore v-if="list.length > 0" :status="status" />
					<view v-if="list.length == 0" class="nodata" v-show="list.length <= 0">
						<view class="contents">
							<image src="../../../../static/image/zanwusheibei.png"></image>
							<view class="text"> 暂无数据</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import DeviceList from './deviceList.vue';
	export default {
		data() {
			return {
				inputX: false,
				inputData: "",
				items: ['全部设备', '在线设备', '离线设备'],
				list: [],
				current: "0",
				listData: {
					page: 1,
					limit: 10,
					querySearchWords: "",
				},
				status: "loading",
				refresherTriggered: false,
			};
		},
		onLoad() {

		},
		onShow() {
			this.getList()
		},
		components: {
			DeviceList
		},
		methods: {
			//充值
			getRecharge(e){
				uni.navigateTo({
					url:"/subpackageA/pages/device/recharge?productId=" + e.id
				})
			},
			//使用
			async getuse(e) {
				let res = await this.$request.get("/water-platform-app-api/product/product/setUse", {id:e.id})
				if (res.code == 0) {
					this.getList()
					uni.showToast({
						title: "使用成功",
						icon: "none"
					})
				}
			},
			//失焦
			async getBlur(e) {
				let data = {
					id: e.id,
					name: e.remark,
				}
				let res = await this.$request.get("/water-platform-app-api/product/product/setName", data)
				if (res.code == 0) {
					this.getList()
					uni.showToast({
						title: "设置成功",
						icon: "none"
					})
				}
			},
			confirm() {
				this.getList()
			},
			clear() {
				this.$set(this.listData, 'querySearchWords', '')
				this.getList()
			},
			refresherrefresh() {
				this.refresherTriggered = true;
				this.status = "loading"
				this.listData.page = 1;
				this.getList()
			},
			scrolltolower() {
				if (this.status == "nomore") return;
				if (!this.loading) {
					this.listData.page++;
					this.status = "loading"
					this.getList(false);
				}
			},
			getList(state = true) {
				this.$request.get("/product/product/list", this.listData).then(res => {
					setTimeout(() => {
						this.loading = false;
					}, 200);
					this.refresherTriggered = false;
					if (res.code == 0) {
						if (this.listData.limit > res.data.length) {
							this.status = "nomore"
						} else {
							this.status = "loadmore"
						}
						if (state) {
							this.list = res.data;
						} else {
							this.list.push(...res.data)
						}

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}

				})
			},
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
				this.listData.page = 1;
				this.status = "loading"
				if (e.currentIndex == 0 && this.listData.networkStatus) {
					delete this.listData.networkStatus;
				}
				if (e.currentIndex == 1) {
					this.listData["networkStatus"] = 1;
				}
				if (e.currentIndex == 2) {
					this.listData["networkStatus"] = 0;
				}
				this.getList()
			},
		}
	}
</script>

<style lang="scss">
	.box {
		min-height: 100vh;
		background-color: #F3F5F6;
		padding-bottom: 30rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;


		.header {
			height: 208rpx;
			background: linear-gradient(360deg, rgba(212, 231, 250, 0) 0%, #D4E7FA 100%);
			overflow: hidden;

			.search {
				width: 438rpx;
				height: 72rpx;
				background: #FFFFFF;
				border-radius: 100rpx 100rpx 100rpx 100rpx;
				transform: translateX(-60rpx);
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0rpx 24rpx;
				box-sizing: border-box;
			}
		}

		.box_content {
			flex: 1;
			overflow: hidden;
			display: flex;
			flex-direction: column;

			.whole {}

			.device {
				flex: 1;
				margin-top: 48rpx;
				width: 100%;
				height: 0px;
				overflow: hidden;

				.content {
					flex: 1;
					overflow: auto;

				}

				.contents {
					.text {}
				}

				.nodata {
					display: grid;
					place-content: center;
					height: 100%;

					.contents {
						display: flex;
						justify-content: center;
						align-items: center;
						flex-direction: column;

						image {
							width: 150px;
							height: 114px;
						}

						.text {
							font-size: 20rpx;
						}
					}

				}
			}
		}
	}
</style>